//看板
angular.module('SmartCanteenApp.boards', ['ngRoute', 'ngTouch'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/boards', {
            templateUrl: 'productionControl/daily_boards.html',
            controller: 'boardsCtrl'
        });
        $routeProvider.when('/user_defined', {	//看板单位自定义
            templateUrl: 'productionControl/user_defined.html',
            controller: 'userDefinedCtrl'
        });
    }])
    .controller('boardsCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
    	$('#datepicker').datepicker({
		      autoclose: true
		});
		$('#datepicker2').datepicker({
		      autoclose: true
		});
		$scope.pagination([])
		$(".modal-dialog").draggable();//为模态对话框添加拖拽
	    $(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
		$scope.selectDate = $scope.getNowFormatDate()
		//发送查询条件 查询看板信息
		$scope.searchBoard = function(){
			$scope.getJson($scope.resultPath+"/wisdomeatery/v1/canteens/"+$scope.canteenId+"/daily_boards/summary?selected_date="+$scope.selectDate,'','get',function(data){
				console.log(data)
				$scope.boards = data.data
				$scope.nextDate = $scope.selectDate
				$scope.pagination($scope.boards)
			})
		}
   		if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
   				$scope.searchBoard()
   		}
		$scope.$on('to-child', function(event,data) {
			//获取从父控制器传来的当前选中食堂
			$scope.canteenId = data;
			$scope.searchBoard()
		});		
		//删除看板
		$scope.delBoard = function(id){
			$scope.delBoardId = id
			$('#delboard').modal('toggle')
		}
		//执行删除看板
		$scope.sureDelBoard = function(){
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/canteens/'+$scope.canteenId+'/daily_boards/'+$scope.delBoardId,'','delete',function(data){
				console.log(data)
				if(data.code == 0){
					$('#delboard').modal('toggle')
					$scope.searchBoard() // 重新查询看板
					$scope.showSuccess()
				}else{
					$scope.showError(data.msg)
				}
			})
			
		}
		//新增看板
		//1.1查询生产计划
		$scope.searchProductPlan = function(){
			console.log($scope.productPlanDate)
			$scope.selectDate = $scope.productPlanDate
			if($scope.productPlanDate == undefined){
				$scope.showError('请选择日期')
				return
			}
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/production_plans?start_time='+$scope.productPlanDate+'&end_time='+$scope.productPlanDate+'&status=1','','get',function(data){
				console.log(data)
				if(data.code == 0){
					$scope.productPlans = data.data
					if(data.data.length == 0){
						$scope.showError('暂无数据')
					}				
				}else{
					$scope.showError(data.msg)
				}
			})
		}
		//1.2选择生产计划
		$scope.choosePlan = function(pid,e){
			console.log(pid)
			$scope.newBoardPlanId = pid
			$('.pPlan').removeClass('activeTr')
			$(e.target).parent().addClass('activeTr')
		}
		//1.3选中生产计划 执行生成看板
		$scope.newBoard = function(){
			$scope.postData = {
				"product_manufacturing_plan_id":$scope.newBoardPlanId,
				"selected_date":$scope.selectDate
			}
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/canteens/'+$scope.canteenId+'/daily_boards/generate',$scope.postData,'post',function(data){
				console.log(data)
				if(data.code == 0){
					$('#newBoard').modal('toggle')
					$scope.searchBoard() // 重新查询看板
					$scope.showSuccess()
				}else{
					$scope.showError(data.msg)
				}
			})
			
		}
		//查询看板详情
		$scope.showDetail = function(pid,wid,managerName,wname,pname){
			console.log($scope.nextDate)
			$scope.detailPostData = {			
				"product_manufacturing_plan_id": pid,
				"workshop_id": wid,
				"selected_date":$scope.nextDate
			}
			$scope.detailmanagerName = managerName
			$scope.detailWname = wname
			$scope.detailPname = pname
			console.log($scope.detailPostData)
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/canteens/'+$scope.canteenId+'/daily_boards',$scope.detailPostData,'post',function(data){
				console.log(data)
				if(data.code == 0){
					$scope.details = data.data
				}else{
					$scope.showError(data.msg)
				}
			})
			$('#boardDetail').modal('toggle')
			//提前查询修改所用的所有食堂员工
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/menus/canteens/'+$scope.canteenId+'/chief','','get',function(data){
				console.log(data)
				if(data.code == 0){
					$scope.cUsers = data.data
				}else{
					$scope.showError('获取信息失败')
				}
			})
		}
		//修改看板
		//1.1 调出修改页面
		$scope.editDetail = function(detail){
			console.log(detail)
			$scope.editName = detail.product_name
			//			detail.id
			if(detail.user_id == null){
				detail.user_id = ''
			}
			$scope.editObj = {
				"user_id": detail.user_id,
				"expected_start_time": detail.expected_start_time,
				"expected_complete_time": detail.expected_complete_time,
				"id":detail.id,
				"product_id":detail.product_id,
				"product_type":detail.product_type,
				"count":detail.count
			}
			$('#showEditDetail').modal('toggle')
		}
		//1.2 执行修改
		$scope.subEdit = function(){
			 if( !isFullTime( $scope.editObj.expected_start_time ) ){
			 	$scope.showError('请填写正确时间格式00:00:00')
			 	return
			 }
			 if( !isFullTime( $scope.editObj.expected_complete_time ) ){
			 	$scope.showError('请填写正确时间格式00:00:00')
			 	return
			 }
			 if( $scope.editObj.expected_complete_time<$scope.editObj.expected_start_time ||$scope.editObj.expected_start_time == $scope.editObj.expected_complete_time){
			 	$scope.showError('结束时间不能小于或等于开始时间')
			 	return
			 }
			 console.log($scope.editObj)
			 $('#showEditDetail').modal('toggle')
			 console.log($scope.detailPostData)
			// 发送修改请求
			 $scope.getJson($scope.resultPath+'/wisdomeatery/v1/canteens/'+$scope.canteenId+'/daily_boards/'+$scope.editObj.id,$scope.editObj,'put',function(data){
			 	if(data.code == 0){
			 		//重新请求数据
					$scope.getJson($scope.resultPath+'/wisdomeatery/v1/canteens/'+$scope.canteenId+'/daily_boards',$scope.detailPostData,'post',function(data){
						console.log(data)
						if(data.code == 0){
							$scope.details = data.data
						}else{
							$scope.showError(data.msg)
						}
					})
			 	}else{
			 		$scope.showError(data.msg)
			 	}
			 })
			
		}
    }])


	.controller('userDefinedCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
		//模板块可拖动
    	$(".modal-dialog").draggable();//为模态对话框添加拖拽
		$(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
		$scope.pagination([])
		$scope.initial = '10'
		//查询加工时长定制记录
		$scope.getWork_time = function(){
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/canteens/'+$scope.canteenId+'/daily_boards/work_time?workshop_id='+$scope.searchId,'','get',function(data){
				$scope.pagination(data.data)
			})
		}
//		查询所有可以自定义的车间
		$scope.getWorkshop = function(){
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/canteens/'+$scope.canteenId+'/daily_boards/workshop','','get',function(data){
				console.log(data)
				$scope.workshops = data.data
				$scope.searchId =data.data[0].id
				$scope.getWork_time()
			})
		}
		//获取当前食堂所有单位
		$scope.getUnits = function(){
			$scope.getJson($scope.resultPath+"/wisdomeatery/v1/menus/canteens/"+$scope.canteenId+"/itemsUnit",'',"GET",function(data){
				$scope.units = data.data
			})
		}
		//获取当前食堂
		if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
   				$scope.getWorkshop()
   				$scope.getUnits()
   		}
		$scope.$on('to-child', function(event,data) {
			//获取从父控制器传来的当前选中食堂
			$scope.canteenId = data;
			$scope.getWorkshop()
			$scope.getUnits()
		});
		
		//生成加工时长定制记录
		$scope.generate = function(){
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/canteens/'+$scope.canteenId+'/daily_boards/work_time/generate','','post',function(data){
				$scope.getWork_time()
			})
		}
		//修改加工时长定制记录（单记录）
		var index = 0
		$scope.editWork_time = function(item,index){
			console.log(index)
			index = index
			$scope.editData = item
			$scope.staticeditData = {
				calculation_convert_rate : item.calculation_convert_rate,
				calculation_unit_id      : item.calculation_unit_id,
				calculation_unit_name	 : item.calculation_unit_name,
				canteen_id				 : item.canteen_id,
				create_time				 : item.create_time,
				id						 : item.id,
				is_new_added:item.is_new_added,
				last_update_time:item.last_update_time,
				manufacturing_unit_id:item.manufacturing_unit_id,
				manufacturing_unit_name:item.manufacturing_unit_name,
				manufacturing_work_time:item.manufacturing_work_time,
				procedure_id:item.procedure_id,
				procedure_name:item.procedure_name,
				product_id:item.product_id,
				product_name:item.product_name,
				product_type:item.product_type,
				workshop_id:item.workshop_id,
			}
			$('#edit').modal('toggle')
		}
		//删除加工时长定制记录（单记录）
		//1. 模态框弹出
		$scope.deleteWork_time = function(item){
			$scope.editData = item
			$('#delete').modal('toggle')
		}
		//模态框点击确定
		$scope.confirm = function(parm){
			console.log($scope.units)
			console.log($scope.editData)
			if(parm == 'delete'){ //执行删除
				$scope.getJson($scope.resultPath+'/wisdomeatery/v1/canteens/'+$scope.canteenId+'/daily_boards/work_time/'+$scope.editData.id,'','delete',function(data){
					$scope.getWork_time()
					$('#delete').modal('toggle')					
				})
//				console.log($scope.editData.id)
//				$('#delete').modal('toggle')
			}else if(parm == 'edit' ){//执行修改
				if($scope.staticeditData.calculation_unit_id == null || 
					$scope.staticeditData.manufacturing_unit_id == null ||
					$scope.staticeditData.manufacturing_work_time == null ||
					$scope.staticeditData.calculation_convert_rate == null){
					$scope.showError('信息不完整')
					return
				}
				var wroktime_validate = checkNumber($scope.staticeditData.manufacturing_work_time)
				var convert_rate_validate = checkNumber($scope.staticeditData.calculation_convert_rate)
				if(wroktime_validate && convert_rate_validate){
					$scope.editData = $scope.staticeditData 
					$scope.getJson($scope.resultPath+'/wisdomeatery/v1/canteens/'+$scope.canteenId+'/daily_boards/work_time/'+$scope.editData.id,$scope.editData,'put',function(data){
						$scope.getWork_time()
						$('#edit').modal('toggle')
					})
				}else{
					$scope.showError('数字格式不正确')
				}
				
			}
		}
		//取消编辑
		$scope.cancel = function(){
			$('#edit').modal('toggle')
		}
	}])

